package it.unibz.ti2.internetPizzeria.servlets;

import it.unibz.ti2.internetPizzeria.dao.OrderDAO;
import it.unibz.ti2.internetPizzeria.dao.PropertyDAO;
import it.unibz.ti2.internetPizzeria.entities.Order;
import it.unibz.ti2.internetPizzeria.entities.Property;
import it.unibz.ti2.internetPizzeria.utils.OrderUtil;
import it.unibz.ti2.internetPizzeria.utils.RouteOptimizer;
import it.unibz.ti2.internetPizzeria.utils.ServletUtil;
import it.unibz.ti2.internetPizzeria.webAttributes.OrderInfo;
import it.unibz.ti2.internetPizzeria.webAttributes.UserInfo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Responsible for retrieving information about currently delivered orders for
 * current user and changing order's status to Delivered or Not delivered.
 * Passing orders list and delivery route waypoints to delivery.jsp page.
 * 
 */
public class DeliveryMap extends HttpServlet{

	private static final long serialVersionUID = 7744961746717790191L;
	
	protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
		this.doPost(arg0, arg1);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		OrderDAO orderDAO = null;
		try {
			orderDAO = new OrderDAO();
			orderDAO.openSession();
			PropertyDAO propertyDAO = new PropertyDAO();
			propertyDAO.openSession(orderDAO);
			String firstWaypoint = propertyDAO.getProperty(Property.PizzeriaAddressKey).getValue();
			float startPointLat = Float.parseFloat(propertyDAO.getProperty(Property.PizzeriaAddressLatKey).getValue());
			float startPointLng = Float.parseFloat(propertyDAO.getProperty(Property.PizzeriaAddressLngKey).getValue());
			
			String statusParam = request.getParameter("status");
			String orderIdParam = request.getParameter("orderId");
			String commentParam = request.getParameter("deliveryComment");
			
			if (statusParam != null && orderIdParam != null){
				Order order = orderDAO.getOrder(Long.parseLong(orderIdParam));				
				order.setStatus(statusParam);
				order.getDelivery().setComment(commentParam);
				if (order.getStatus().equals(Order.StatusDelivered)){
					order.getDelivery().setDeliveredDate(new Date());
				}
				orderDAO.update(order);
				firstWaypoint = order.getDelivery().getAddress();
				startPointLat = order.getDelivery().getPositionLat();
				startPointLng = order.getDelivery().getPositionLng();
			}
			
			UserInfo userInfo = (UserInfo)request.getSession().getAttribute("userInfo");
			List<Order> orders = orderDAO.getOrdersInDelivery(userInfo.getId());
			if (orders.size() > 0){
				orders = RouteOptimizer.optimize(startPointLat, startPointLng, orders);
			}
			List<String> waypoints = new ArrayList<String>();
			waypoints.add(firstWaypoint);
			for (Order order : orders) {
				waypoints.add(order.getDelivery().getAddress());
			}
			waypoints.add(propertyDAO.getProperty(Property.PizzeriaAddressKey).getValue());
			List<OrderInfo> orderInfos = OrderUtil.getOrderInfos(orders);

			request.setAttribute("waypoints", waypoints);
			request.setAttribute("orderInfos", orderInfos);
			
			ServletUtil.redirect(request, response, "/deliveryMap.jsp");			
			
		} catch (Exception ex) {			
			ex.printStackTrace();
		} finally {
			if (orderDAO != null){
				orderDAO.closeSession();
			}
		}
	}
}
